package cn.glutnn.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.InputStream;
import java.util.Random;

/**
 * @Title: AliyunOSS
 * @Author QJQ
 * @Package cn.glutnn.utils
 * @Date 2024/11/1 11:07
 * @description: 阿里云oss对象存储示例
 */

public class AliyunOSS {
    private static final String BUCKET_NAME = "rxtong";
    private static final String OSS_ACCESS_KEY_ID="LTAI5tMoh84GEk8h242arEAN";
    private static final String OSS_ACCESS_KEY_SECRET="aZAeALYoJQe1vXk5Tjtg92zawoPkWt";
    private static final String END_POINT="https://oss-cn-guangzhou.aliyuncs.com";
//    private static final String END_POINT="https://oss-cn-beijing.aliyuncs.com";

    public static String uploadFile(String objectName, InputStream input) throws Exception {
        String url="";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(END_POINT, OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET);
        url="https://"+BUCKET_NAME+"."+END_POINT.substring(END_POINT.lastIndexOf("/")+1)+"/"+objectName;
        try {
            ///////////
//            Random random=new Random();
//            int i=random.nextInt();
//            //判断是否已存在某个文件
//            if(ossClient.doesObjectExist(BUCKET_NAME,objectName)){
//            objectName=objectName;
//            }
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, objectName, input);
            // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
            // ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
            // metadata.setObjectAcl(CannedAccessControlList.Private);
            // putObjectRequest.setMetadata(metadata);

            // 上传文件。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
        } catch (OSSException oe) {
            System.err.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.err.println("Error Message:" + oe.getErrorMessage());
            System.err.println("Error Code:" + oe.getErrorCode());
            System.err.println("Request ID:" + oe.getRequestId());
            System.err.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.err.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.err.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return url;
    }

}
